package com.alliky.home.ui.fragment

import android.os.Bundle
import android.view.View
import android.widget.ImageView
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.lifecycleScope
import androidx.paging.ExperimentalPagingApi
import androidx.paging.LoadState
import androidx.recyclerview.widget.GridLayoutManager
import coil.load
import coil.transform.RoundedCornersTransformation
import com.alibaba.android.arouter.facade.annotation.Route
import com.alliky.common.router.RoutePath
import com.alliky.core.base.BaseFragment
import com.alliky.core.callback.OnItemClickListener
import com.rrc.common.entity.BannerInfo
import com.rrc.common.entity.BannerVO
import com.rrc.common.entity.CategoryVO
import com.rrc.common.entity.GoodsVO
import com.alliky.core.net.IStateObserver
import com.alliky.home.R
import com.alliky.home.ui.adapter.CategoryAdapter
import com.alliky.home.ui.adapter.HomeHotsAdapter
import com.alliky.home.databinding.FragmentHomeBinding
import com.alliky.home.ui.viewmodel.HomeViewModel
import kotlinx.android.synthetic.main.fragment_home.*
import kotlinx.coroutines.flow.collectLatest

@ExperimentalPagingApi
@Route(path = RoutePath.Home.FRAGMENT_HOME)
class HomeFragment : BaseFragment<FragmentHomeBinding>() {

    private val mViewModel: HomeViewModel by activityViewModels()

    var mHomeHotAdapter: HomeHotsAdapter = HomeHotsAdapter()

    var mCategoryAdapter: CategoryAdapter = CategoryAdapter(ArrayList())

    override fun getLayoutId(): Int {
        return R.layout.fragment_home
    }

    override fun initData(savedInstanceState: Bundle?) {

        mBinding.xbanner.loadImage { banner, model, view, position ->
            (view as ImageView).load((model as BannerInfo).imageUrl) {
                transformations(RoundedCornersTransformation(16f))
            }
        }

        mBinding.recyclerViewHot.apply {
            layoutManager = GridLayoutManager(requireContext(), 2)
            adapter = mHomeHotAdapter
        }

        mHomeHotAdapter.setOnItemClickListener(object : OnItemClickListener<GoodsVO> {
            override fun onItemClick(view: View?, item: GoodsVO?, position: Int) {
//                RxToast.normal(item?.goods_name)
            }
        })

        mBinding.recyclerViewClassify.apply {
            layoutManager = GridLayoutManager(requireContext(), 5)
            adapter = mCategoryAdapter
        }

        mBinding.refreshLayout.setOnRefreshListener {
            refreshLayout.postDelayed({
                mHomeHotAdapter.refresh()
            }, 500)
        }

        lifecycleScope.launchWhenCreated {
            mHomeHotAdapter.loadStateFlow.collectLatest {
                mBinding.refreshLayout.isRefreshing = it.refresh is LoadState.Loading
            }
        }

        mViewModel.getBanner("1", requireContext())

        mViewModel.getCategory(HashMap())

        lifecycleScope.launchWhenCreated {
            mViewModel.hotGoodsPagingFlow().collectLatest { data ->
                mHomeHotAdapter!!.submitData(data)
            }
        }
    }

    override fun initObservable() {
        mViewModel.bannerLiveData.observe(this, object : IStateObserver<List<BannerVO>>(null) {
            override fun onDataChange(data: List<BannerVO>?) {
                super.onDataChange(data)
                var bannerList = arrayListOf<BannerInfo>()
                for (info in data!!) {
                    var bannerInfo = BannerInfo()
                    bannerInfo.imageUrl = info.thumb
                    bannerInfo.title = info.title
                    bannerList.add(bannerInfo)
                }
                mBinding.xbanner.setBannerData(bannerList)
            }
        })

        mViewModel.categoryListData.observe(this, object : IStateObserver<List<CategoryVO>>(null) {
            override fun onDataChange(data: List<CategoryVO>?) {
                super.onDataChange(data)
                mCategoryAdapter?.setNewData(data)
            }
        })
    }
}